package com.sun.wanandroid.ui.splash

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.sun.module_base.model.AppThemeState
import com.sun.module_base.theme.SystemUiController
import com.sun.module_base.widget.BaseScreen
import com.sun.wanandroid.R
import dagger.hilt.android.AndroidEntryPoint

/**
 * 利用给SplashActivity设置带有背景的SplashTheme来避免启动白屏的问题
 */
@SuppressLint("CustomSplashScreen")
@AndroidEntryPoint
class SplashActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            val systemUiController = remember { SystemUiController(window) }
            val appTheme = remember { mutableStateOf(AppThemeState()) }
            BaseScreen(appTheme.value, systemUiController) {
                val viewModel: SplashViewModel = hiltViewModel()
                Box(
                   modifier =  Modifier
                        .fillMaxSize()
                        .background(MaterialTheme.colorScheme.onPrimary),
                     contentAlignment = Alignment.Center) {
                    Image(
                        painter = painterResource(id = R.mipmap.ic_launcher_round),
                        contentDescription = null,
                        modifier = Modifier.size(200.dp)
                    )
                }
                viewModel.ExecuteCurrent()

            }
        }
    }

    /**
     * 在SplashActivity的onStop()方法中调用finish()的好处主要体现在资源释放和避免白屏问题：
     */
    override fun onStop() {
        super.onStop()
        finish()
    }
}
